/*-------------------------------------------------01.对话框:消息框-------------------------------------------------*/

/**
 * 对话框:提示消息框
 *
 * @param str
 */
 var openShortAlertInfoTimer = null;
 var openShortAlertMessageTimer = null;
 var openShortAlertErrorTimer = null;
 var openShortSuccessInfoTimer = null;
function openShortAlertInfo(str)
{
	var position = getDialogCenterPos(window, 95, 170);


	var html = '<div class="row"   id="aicfShortAlertInfoDiv"> <div style="z-index:9999999999; position:absolute; top:' + position.top + '; left:' + position.left
	+ ';" class="span11"><div><div class="tips tips-large tips-success">'+
	'<span class="x-icon x-icon-success"><i class="icon icon-ok icon-white"></i></span>'+
	'<div class="tips-content"><h2>'+str+'</h2>'+

	'</div></div></div></div><div class="row">';

	$("body:eq(0)").append(html);
	if(openShortAlertInfoTimer) clearTimeout(openShortAlertInfoTimer);
	openShortAlertInfoTimer = setTimeout("hideAICFDialog('aicfShortAlertInfoDiv');", 1000);
}

function openShortAlertMessage(str)
{
	var position = getDialogCenterPos(window, 95, 170);


	var html = '<div class="row"   id="aicfShortAlertInfoDiv"> <div style="z-index:9999999999; position:absolute; top:' + position.top + '; left:' + position.left
	+ ';" class="span11"><div><div class="tips tips-large x-icon-warning">'+
	'<span class="x-icon x-icon-warning"><i class="icon icon-ok icon-white"></i></span>'+
	'<div class="tips-content"><h2>'+str+'</h2>'+

	'</div></div></div></div><div class="row">';

	$("body:eq(0)").append(html);
	if(openShortAlertMessageTimer) clearTimeout(openShortAlertMessageTimer);
	openShortAlertMessageTimer = setTimeout("hideAICFDialog('aicfShortAlertInfoDiv');", 5000);
}

function openShortAlertError(str)
{
	if($('#shortAlertInfoDiv').length) return;
	var position = getDialogCenterPos(window,95, 170);

	var html = '<div class="row"   id="shortAlertInfoDiv"> <div style="z-index:9999999; position:absolute; top:' + position.top+ '; left:' + position.left
	+ ';" class="span6"><div><div class="tips tips-small tips-warning">'+
	'<span class="x-icon x-icon-error">x</span>'+
	'<div class="tips-content"><h2>'+str+'</h2>'+

	'</div></div></div></div><div class="row">';

	$("body:eq(0)").append(html);

	setTimeout("hideAICFDialog('shortAlertInfoDiv');", 2000);
}

/**
 * 对话框:提示消息框
 *
 * @param str
 */
function openShortSuccessInfo(handler, str, msg)
{
	str = str || '操作成功！';
	msg = msg || '';

	var position = getDialogCenterPos(window, 360, 44);

	var html = '<div id="aicfShortSuccessInfoDiv" class="tishi_Rom" style="z-index:9999999; position:absolute; top:' + position.top + '; left:' + position.left
			+ ';"><div class="Rom1"><p>' + str + '<span>' + msg + '</span></p></div><div class="Rom4"></div><div class="Rom5"></div></div>';

	$("body:eq(0)").append(html);
	if(openShortSuccessInfoTimer) clearTimeout(openShortSuccessInfoTimer);
	openShortSuccessInfoTimer = setTimeout(function()
	{
		hideAICFDialog('aicfShortSuccessInfoDiv');
		if (handler)
		{
			handler();
		}
	}, 500);
}

/**
 * 对话框:处理中消息框
 *
 * @param str
 */
var AICF_SHORT_LOADING = false;
function toggleShortLoadingInfo(str)
{
    var obj = document.getElementById("aicfShortLoadingInfoDiv");
	if (AICF_SHORT_LOADING||obj!=null)
	{
		// $("#aicfShortLoadingInfoDiv").remove();


		obj.parentNode.removeChild(obj);
		AICF_SHORT_LOADING = false;
		return;
	}

	str = str || '请稍候，正在提交你的请求...';

	var position = getDialogCenterPos(window, 360, 44);

	var img = AICF_CTX + "/v3/images/loading.gif";
	var html = '<div id="aicfShortLoadingInfoDiv" style="z-index:9999999; position:absolute; top:' + position.top + '; left:' + position.left
			+ ';" class="tishi_Rom"><div class="Rom1_1"><div class="left"><img src="' + img + '" width="32" height="32" /></div><div class="right">' + str
			+ '</div></div><div class="Rom4"></div><div class="Rom5"></div></div>';
	var oDiv = document.createElement("div");
	oDiv.innerHTML = html;

	document.body.appendChild(oDiv);

	// $("body:eq(0)").append(html);
	AICF_SHORT_LOADING = true;

}


//隐藏提示信息框 by zhoucy2

function hideShortLoadingInfo(){

		var obj=document.getElementById("aicfShortLoadingInfoDiv");
		if(obj!=null)
		obj.parentNode.removeChild(obj);
		//AICF_SHORT_LOADING = false;

}

/*-------------------------------------------------02.对话框:确认框,HTML窗口-------------------------------------------------*/

/**
 * @param msg
 * @param ok
 * @param cancel
 */
function openSelfAlertDialog(msg, ok)
{
	var html = '<table cellpadding="0px" cellspacing="0px" border="0px"><tr><td class="txt del red">' + msg + '</td><td class="cenR"></td></tr></table>';
	showAICFDialogInWindow("操作提示", html, ok, null);
}

/**
 * 对话框:提示确认窗口[window,当前窗口]
 *
 * @param msg
 * @param ok
 * @param cancel
 */
function openSelfConfirmDialog(msg, ok, cancel)
{
	var html = '<table cellpadding="0px" cellspacing="0px" border="0px"><tr><td class="txt tan">' + msg + '</td><td class="cenR"></td></tr></table>';
	showAICFDialogInWindow("操作提示", html, ok, cancel);
}

/**
 * 对话框:提示确认窗口[window.top,顶层窗口]
 */
function openTopConfirmDialog(msg, ok, cancel)
{
	top.invokeWindow = window;
	top.openSelfConfirmDialog(msg, ok, cancel);
}

/**
 * 对话框:加载URL页面窗口[window,当前窗口]
 *
 * <P>
 * ok回调函数若返回false，则窗口会继续停留；否则默认关闭
 * </P>
 *
 * @param t
 *            标题
 * @param u
 *            URL地址
 * @param ok
 *            确定按钮回调函数
 * @param cancel
 *            取消按钮回调函数
 * @param w
 *            宽，默认500px
 * @param h
 *            高，默认200px
 */
function openSelfHtmlDialog(t, u, ok, cancel, w, h)
{
	$.get(u.url(), function(data)
	{
		showAICFDialogInWindow(t, data, ok, cancel, w, h);
	});
}

/**
 * 显示对话框[window,当前窗口]
 *
 * @param t
 *            标题
 * @param h
 *            HTML源码
 * @param ok
 *            确定回调接口
 * @param cancel
 *            取消回调接口
 * @param w
 *            宽度
 * @param h
 *            高度
 */
function showAICFDialogInWindow(t, html, ok, cancel, w, h)
{
	h = h || 200;
	w = w || 360;

	var dialogId = "AICFDialog_" + new Date().getTime();
	var selfD_ok;
	var position = getDialogCenterPos(window, w, h);

	var div = '<div id="'
			+ dialogId
			+ '" class="tishi_pop" style="width:'
			+ w
			+ 'px;z-index:9999999; position:absolute; top:'
			+ position.top
			+ '; left:'
			+ position.left
			+ ';"><div class="topL"><h3>'
			+ t
			+ '</h3></div><div class="topR"><a href="javascript:void(0);" onclick=\'hideAICFDialog("'
			+ dialogId
			+ '", 500);\'>&nbsp;</a></div><div class="center">'
			+ html
			+ '</div><div class="bottomL"><div class="ti"><a class="selfD_ok" href="javascript:void(0);">确定</a><a class="selfD_no" href="javascript:void(0);">取消</a></div></div><div class="bottomR"></div></div>';

	var alpahaDivId = dialogId + "alpaha";
	var alphaDiv = '<div id="' + alpahaDivId + '" class="tishi_pop" style="background:#000000;width:' + position.pageWidth + 'px;height:' + position.pageHeight
			+ 'px; z-index:9999999 position:absolute; top:0 ; left:0;opacity:0.3;filter:Alpha(opacity=10)">'
			+ '<iframe style="background:#000000;filter:Alpha(opacity=10);width:' + position.pageWidth + ';height:' + position.pageHeight
			+ ';top:0;left:0"></iframe>' + '</div>';
	$("body:eq(0)").append(alphaDiv);

	$("body:eq(0)").append(div);

	var okHandler = function()
	{
		var result = true;
		if (ok)
		{
			result = ok();
			if (result == null)
			{
				result = true;
			}
		}

		if (result)
		{
			hideAICFDialog(dialogId, 500);
		}
	};
	var btnOk = $("#" + dialogId + " .selfD_ok");
	btnOk.bind("click", okHandler);
	btnOk.focus();

	btnOk.onkeypress = function(event)
	{
		if (event.keyCode == '13')
		{
			btnOk.click();
		}
	};

	var cancelHandler = function()
	{
		if (cancel)
		{
			cancel();
		}
		hideAICFDialog(dialogId, 500);
	};
	$("#" + dialogId + " .selfD_no").bind("click", cancelHandler);
}

/**
 * 隐藏对话框
 *
 * @param id
 * @param interval
 */
function hideAICFDialog(id, interval)
{
	interval = interval || 1000;
	$("#" + id).hide(interval, function()
	{
		if (id + "alpaha")
		{
			$("#" + id + "alpaha").remove();
		}
		$("#" + id).remove();
	});
}

/*-------------------------------------------------03.对话框:URL嵌入IFrame窗口-------------------------------------------------*/

var invokeWindow = null;

function getInvokeWindow()
{
	return top.invokeWindow;
}

/**
 * 对话框:嵌入URL窗口[window,当前窗口]
 *
 * @param title
 * @param url
 * @param width
 * @param height
 */
function openSelfUrlDialog(title, url, width, height)
{
	openUrlDialog(window, title, url, width || 1000, height || 450);
}

/**
 * 对话框:嵌入URL窗口[top,顶层窗口,iframe方式,只读]
 *
 * @param title
 * @param url
 * @param width
 * @param height
 */
function openTopViewIframeDialog(title, url, width, height)
{
	var url = "/aicf/iframeDialogComponent.action?dialogUrl=" + escape(url);
	openTopUrlDialog(title, url, width, height);
}

/**
 * 对话框:嵌入URL窗口[top,顶层窗口,include方式,只读]
 *
 * @param title
 * @param url
 * @param width
 * @param height
 */
function openTopViewIncludeDialog(title, url, width, height)
{
	var url = "/aicf/includeDialogComponent.action?dialogUrl=" + escape(url);
	openTopUrlDialog(title, url, width, height);
}

/**
 * 对话框:嵌入URL窗口[top,顶层窗口]
 *
 * @param title
 * @param url
 * @param width
 * @param height
 */
function openTopUrlDialog(title, url, width, height)
{
	if (url.indexOf("?") == -1)
	{
		url += "?";
	} else
	{
		url += "&";
	}

	url = url + 'isTop=true';
	openUrlDialog(window.top, title, url, width || 1000, height || 450);
}

function openUrlDialog(src, title, url, width, height)
{
	top.invokeWindow = window;

	var position = getDialogCenterPos(src, width, height);

	var alphaDiv = src.document.createElement("div");
	alphaDiv.setAttribute("id", "dialogAlphaDiv");
	alphaDiv.style.background = "#000000";
	alphaDiv.style.width = position.pageWidth;
	alphaDiv.style.height = position.pageHeight;
	alphaDiv.style.position = "absolute";
	alphaDiv.style.top = "0";
	alphaDiv.style.left = "0";
	alphaDiv.style.zIndex = "500";
	alphaDiv.style.opacity = "0.3";
	alphaDiv.style.filter = "Alpha(opacity=30)";
	alphaDiv.innerHTML = '<iframe style="background:#000000;filter:Alpha(opacity=10);width:' + position.pageWidth + ';height:' + position.pageHeight
			+ ';top:0;left:0"></iframe>';
	src.document.body.appendChild(alphaDiv);

	var dialogDiv = src.document.createElement("div");
	// var dialogDiv = window.document.createElement("div");
	dialogDiv.setAttribute("id", "dialogInfoDiv");
	dialogDiv.setAttribute("class", "pop1");
	dialogDiv.setAttribute("className", "pop1");
	dialogDiv.style.width = width + "px";
	dialogDiv.style.height = height + "px";
	dialogDiv.style.zIndex = "1000";
	dialogDiv.style.position = "absolute";

	dialogDiv.style.top = ($(src.document).scrollTop() + ($(src).height() - height) / 2) + "px";
	dialogDiv.style.left = ((position.pageWidth - width) / 2) + "px";
	var loadurl = '/v3/component/dialog/loading.jsp';
	var dialogObj = '<h3 id="dragH3">' + title + '</h3><a href="javascript:void(0);" onclick="closeUrlDialog();" class="close">&nbsp;</a>'
			+ '<div  style="width:' + width + 'px;">' + '<iframe src="' + loadurl.url() + '" frameBorder="0" width="100%" height="' + (height - 30)
			+ '" scrolling="no" allowtransparency="true"  id="dialogLoadingFrame"></iframe>' + '<iframe style="display:none;" src="' + url.url()
			+ '" frameBorder="0" width="100%" height="' + (height - 30) + '" scrolling="no" allowtransparency="true"  id="dialogFrame"></iframe>'
			+ '<div class="con4"></div></div>';

	dialogDiv.innerHTML = dialogObj;

	src.document.body.appendChild(dialogDiv);
	// window.document.body.appendChild(dialogDiv);
	// $("#tophide_btn").after(dialogDiv);
	// $("#dialogInfoDiv").easydrag();
	if (window.event)
	{
		window.event.returnValue = false;
	}

	$("#dialogFrame", src.document).load(function()
	{
		$("#dialogLoadingFrame", src.document).remove();
		$("#dialogFrame", src.document).show();

	});
	// var dragH3 = src.document.getElementById("dragH3");
	var docMouseMoveEvent = src.document.onmousemove;
	var docMouseUpEvent = src.document.onmouseup;
	var docMouseOutEvent = src.document.onmouseout;
	var iWidth = document.documentElement.clientWidth;
	var iHeight = document.documentElement.clientHeight;
	dialogDiv.onmousedown = function()
	{
		var evt = src.event;
		moveable = true;
		moveX = evt.clientX;
		moveY = evt.clientY;
		moveTop = parseInt(dialogDiv.style.top);
		moveLeft = parseInt(dialogDiv.style.left);
		dialogDiv.style.cursor = "move";

		dialogDiv.onmousemove = function()
		{
			if (moveable)
			{
				var evt = src.event;
				var x = moveLeft + evt.clientX - moveX;
				var y = moveTop + evt.clientY - moveY;
				// if ( x > 0 &&( x + width < iWidth) && y > 0 && (y + height <
				// iHeight) ) {
				dialogDiv.style.left = x + "px";
				dialogDiv.style.top = y + "px";
				// }
			}
		};
		dialogDiv.onmouseup = function()
		{
			if (moveable)
			{
				src.document.onmousemove = docMouseMoveEvent;
				src.document.onmouseup = docMouseUpEvent;
				src.document.onmouseout = docMouseOutEvent;
				moveable = false;
				moveX = 0;
				moveY = 0;
				moveTop = 0;
				moveLeft = 0;
			}
		};
		dialogDiv.onmouseout = function()
		{
			if (moveable)
			{
				src.document.onmousemove = docMouseMoveEvent;
				src.document.onmouseup = docMouseUpEvent;
				src.document.onmouseout = docMouseOutEvent;
				dialogDiv.style.cursor = "";
				moveable = false;
				moveX = 0;
				moveY = 0;
				moveTop = 0;
				moveLeft = 0;
			}
		};

	}

}

function test()
{

}

function closeUrlDialog()
{
	$("#dialogFrame").attr("src", "");
	var obj = document.getElementById("dialogFrame");

	if(obj!=null){
		obj.contentWindow.document.write('');
		obj.contentWindow.close();
	}
	obj = null;
	$("#dialogAlphaDiv").remove();
	$("#dialogInfoDiv").remove();

	top.invokeWindow = null;
	if ($.browser.msie)
	{
		CollectGarbage();
	}
}

/*-------------------------------------------------04.对话框:公共方法-------------------------------------------------*/

/**
 * 获取对话框居中位置
 *
 * @param win
 * @param width
 * @param height
 * @returns 位置
 */
function getDialogCenterPos(src, width, height)
{
	var pageHeight = src.document.body.scrollHeight;

	var pageWidth = $(src).width();

	var p = {};
	p.pageHeight = Math.max(pageHeight, $(src).height());
	p.pageWidth = pageWidth;
	p.top = ($(src.document).scrollTop() + ($(src).height() - height) / 2) + "px";
	p.left = ((pageWidth - width) / 2) + "px";

	return p;
}
